import BaseItemView from "../../../cfw/mvc/BaseItemView";
import SnowManManager from "../SnowManManager";
import SnowToyModel from "../model/SnowToyModel";
import ModuleManager from "../../../cfw/module/ModuleManager";
import { ResType } from "../../../cfw/res/ResInterface";
import ResItem from "../../../cfw/res/ResItem";
import SnowBodyModel from "../model/SnowBodyModel";
import GlobalEvent from "../../../cfw/event/GlobalEvent";
import { EventName } from "../../../public/Config";

const { ccclass, property } = cc._decorator;

@ccclass
export default class SnowManView extends BaseItemView {



    @property([cc.Sprite])
    toys: cc.Sprite[] = []

    @property([cc.Sprite])
    bodys: cc.Sprite[] = []

    @property([cc.Sprite])
    backs: cc.Sprite[] = []


    start() {
        // let toyList = SnowManManager.instance().getUseToy()
        for (let index = 0; index < this.toys.length; index++) {
            const element: SnowToyModel = SnowManManager.instance().getUseToy(index)
            if (element) {
                ModuleManager.loadRes(element.getIcon(), ResType.SpriteFrame, (err, res: ResItem) => {
                    if (err || !cc.isValid(this.node)) {
                        return;
                    }
                    this.toys[index].spriteFrame = res.getRes();
                })
            }
        }
        for (let index = 0; index < this.bodys.length; index++) {
            // const element = SnowManManager.instance().getUseBoy(index)
            this.updateItem(index)

        }
        GlobalEvent.instance().on(EventName.SELECT_SNOW, this.updateItem, this)
    }

    onDestroy() {
        GlobalEvent.instance().off(EventName.SELECT_SNOW, this.updateItem, this)
    }

    updateItem(index: number) {
        cc.log(' upateItem index ', index)
        let element: SnowBodyModel = SnowManManager.instance().getUseBoy(index)
        if (element) {
            let offsetX = element.getOffset()
            ModuleManager.loadRes(element.getIcon(), ResType.SpriteFrame, (err, res: ResItem) => {
                if (err || !cc.isValid(this.node)) {
                    return;
                }
                this.bodys[index].spriteFrame = res.getRes();
                if (offsetX && offsetX.length >= 2) {
                    cc.log(' offsetX ', offsetX)
                    this.bodys[index].node.x = -offsetX[0]
                    this.bodys[index].node.y = offsetX[1]
                }

            })
            if (this.backs[index]) {
                ModuleManager.loadRes(element.getBack(), ResType.SpriteFrame, (err, res: ResItem) => {
                    if (err || !cc.isValid(this.node)) {
                        return;
                    }
                    this.backs[index].spriteFrame = res.getRes();
                    if (offsetX && offsetX.length >= 2) {
                        this.backs[index].node.x = offsetX[0]
                        this.backs[index].node.y = offsetX[1]
                    }

                })
            }
        } else {
            this.bodys[index].spriteFrame = null;
            if (this.backs[index]) {
                this.backs[index].spriteFrame = null;
            }
        }
    }
}
